package cloud.tianai.mate.captcha.interceptor.impl;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.util.FontUtils;
import cloud.tianai.captcha.generator.AbstractImageCaptchaGenerator;
import cloud.tianai.captcha.generator.common.model.dto.CaptchaExchange;
import cloud.tianai.captcha.generator.common.model.dto.ClickImageCheckDefinition;
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
import cloud.tianai.captcha.interceptor.Context;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import cloud.tianai.mate.captcha.common.constant.MateCaptchaTypeConstant;
import cloud.tianai.mate.captcha.generator.impl.AbstractClickImageCaptchaGenerator;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author: 天爱有情
 * @date 2024/3/13 16:54
 * @Description 文字点选增加干扰项
 */
public class WordImageClickInterfereProcessor implements CaptchaInterceptor {
    private static final List<Color> randomColors = new ArrayList<Color>() {
        {
            add(new Color(255, 255, 255));
//            add(new Color(88, 140, 241));
//            add(new Color(254, 67, 101));
//            add(new Color(252, 157, 154));
//            add(new Color(249, 205, 173));
//            add(new Color(200, 200, 169));
//            add(new Color(131, 175, 155));
        }
    };

    @Override
    public String getName() {
        return "word_image_interfere_processor";
    }

    @Override
    public void beforeWrapImageCaptchaInfo(Context context, CaptchaExchange captchaExchange, AbstractImageCaptchaGenerator generator) {
        GenerateParam param = captchaExchange.getParam();
        if (!CaptchaTypeConstant.WORD_IMAGE_CLICK.equals(param.getType())
                && !MateCaptchaTypeConstant.WORD_ORDER_IMAGE_CLICK.equals(param.getType())) {
            return;
        }

        List<ClickImageCheckDefinition> transferData = (List<ClickImageCheckDefinition>) captchaExchange.getTransferData();
        BufferedImage bgImage = captchaExchange.getBackgroundImage();
        ThreadLocalRandom random = ThreadLocalRandom.current();
//        Color randomColor = CaptchaImageUtils.getRandomColor(random);
        Color randomColor = getRandomColor(random);
        AbstractClickImageCaptchaGenerator clickImageCaptchaGenerator = (AbstractClickImageCaptchaGenerator) generator;
        int interfereCount = Math.min(2, transferData.size());
        List<ClickImageCheckDefinition> clickImageCheckDefinitionList2 = new ArrayList<>(transferData);
        Collections.shuffle(clickImageCheckDefinitionList2);
        for (int i = 0; i < interfereCount; i++) {
            String randomWord = FontUtils.getRandomChar(random);
            AbstractClickImageCaptchaGenerator.ImgWrapper clickImg = clickImageCaptchaGenerator.getClickImg(new Resource(null, randomWord), randomColor);
//            BufferedImage bufferedImage = clickImageCaptchaGenerator.obfuscateImage(clickImg.getImage(), param);
            BufferedImage bufferedImage = clickImg.getImage();
            ClickImageCheckDefinition checkDefinition = clickImageCheckDefinitionList2.remove(0);
            int x = checkDefinition.getX() - clickImg.getImage().getWidth() / 2;
            int y = checkDefinition.getY() - clickImg.getImage().getHeight() / 2;
            int randomX = 0;
            int randomY = 0;
            if (random.nextBoolean()) {
                randomX = random.nextInt(x - clickImg.getImage().getWidth() / 4, x - clickImg.getImage().getWidth() / 5);
            } else {
                randomX = random.nextInt(x + clickImg.getImage().getWidth() / 5, x + clickImg.getImage().getWidth() / 4);
            }
            if (random.nextBoolean()) {
                randomY = random.nextInt(y - clickImg.getImage().getHeight() / 4, y - clickImg.getImage().getHeight() / 5);
            } else {
                randomY = random.nextInt(y + clickImg.getImage().getHeight() / 5, y + clickImg.getImage().getHeight() / 4);
            }
            CaptchaImageUtils.overlayImage(bgImage, bufferedImage, randomX, randomY);
        }
    }


    private Color getRandomColor(ThreadLocalRandom random) {
        return randomColors.get(random.nextInt(0, randomColors.size()));
    }
}
